package exception;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 异常处理机制在io中的引用
 */
public class FinallyDemo2 {
    public static void main(String[] args) {
        //流
        FileOutputStream fos=null;
        /*try {
            fos=new FileOutputStream("fos.txt");
            fos.write(1);
        }catch (IOException e){
            System.out.println("异常处理");
        }finally {
            try {
                if (fos!=null){
                    fos.close();
                }
            }catch (IOException e){
                System.out.println("关闭流异常");
            }
        }*/

        //ctrl+alt+t 自动添加异常处理
        try {
            fos=new FileOutputStream("fos.txt");
            fos.write(1);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (fos!=null){
                    fos.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
